package com.hcrazy.manchester.app.services;

import java.util.List;

import com.hcrazy.manchester.app.jdbc.ForumDAO;
import com.hcrazy.manchester.app.jdbc.ModeratorDAO;
import com.hcrazy.manchester.app.jdbc.UserDAO;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.Forum;
import com.hcrazy.manchester.app.pojo.Moderator;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.utils.Constant;

public class ForumService {

	private ForumDAO forumDAO;
	
	private ModeratorDAO moderatorDAO;
	
	private UserDAO userDAO;

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public ModeratorDAO getModeratorDAO() {
		return moderatorDAO;
	}

	public void setModeratorDAO(ModeratorDAO moderatorDAO) {
		this.moderatorDAO = moderatorDAO;
	}

	public ForumDAO getForumDAO() {
		return forumDAO;
	}

	public void setForumDAO(ForumDAO forumDAO) {
		this.forumDAO = forumDAO;
	}

	public ErrorMsg addOrUpdateForum(Forum forum) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		if(forum.getForumId() > 0){
			forumDAO.updateForum(forum);
		}else{
			forumDAO.addForum(forum);
		}
		
		return msg;
	}
	
	public List<Forum> getAllForums() throws Exception{
		List<Forum> list = forumDAO.listAllForums();//论坛列表(非群组)
		//为论坛版块添加版主列表
		for(Forum f:list){
			List<String> ms = moderatorDAO.listUsernamesByForumId(f.getForumId());
			f.setModerators(ms);
		}
		Forum root = new Forum();
		root.setForumId(0);
		setChildren(root, list);
		
		return root.getChildren();
	}
	
	private void setChildren(Forum root,List<Forum> list){
		for(Forum forum:list){
			if(forum.getParentId() == root.getForumId()){
				setChildren(forum, list);
				root.getChildren().add(forum);
			}
		}
	}
	
	public String delForum(int forumId) throws Exception{
		
		//删除子版块
		forumDAO.delForumByParentId(forumId);
		
		//删除本版块
		forumDAO.delForum(forumId);
		
		//清空所有父分类不存在的分类
		
		return null;
	}
	
	/**
	 * 论坛版主列表
	 * @param forumId
	 * @return
	 * @throws Exception
	 */
	public List<Moderator> getModeratorsByForumId(int forumId) throws Exception{
		return moderatorDAO.listModeratorsByForumId(forumId);
	}
	
	public ErrorMsg addModerator(Moderator moderator) throws Exception{
		//判断用户是否存在
		String username = moderator.getUsername();
		User user = userDAO.getUserByUsername(username);
		if(null == user){
			return new ErrorMsg(Constant.FORUM_MODERATOR_ADD_USERNAME_NULL,"指定用户不存在！");
		}
		moderatorDAO.addModerator(moderator);
		return new ErrorMsg(Constant.SUCCESS);
	}
	
	public void delModerator(int forumId,String username) throws Exception{
		moderatorDAO.delModerator(forumId, username);
	}
	
	public Forum getForumById(int forumId) throws Exception{
		return forumDAO.getForumById(forumId);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,int sortId) throws Exception{
		return forumDAO.getPagedInfos(model, search, sortId);
	}
}
